package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.ejia.pojo.fytype;
import com.ejia.server.MoneyService;
import com.ejia.server.MtypeService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.JSeparator;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SFLX extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private JTextField txtName;
	private JTextField txtPrice;
	private JTextField txtUnit;
	public static ArrayList<fytype> fytypes;
	public static int index;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SFLX frame = new SFLX();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void dataBind() {
		fytypes = MtypeService.find();
		Object[][] data =new Object[fytypes.size()][];
		for (int i = 0; i < data.length; i++) {
			data[i]=new Object[] {
					fytypes.get(i).getFyid(),fytypes.get(i).getFyname(),fytypes.get(i).getFyprice(),fytypes.get(i).getUnit()
			};
		}
		table.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u7F16\u53F7", "\u540D\u79F0", "\u5355\u4EF7", "\u5355\u4F4D"
				}
			));
		table.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 26));
	}
	
	/**
	 * Create the frame.
	 */
	public SFLX() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(SFLX.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("收费类型管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1326, 675);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("刷新数据");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			dataBind();
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 24));
		btnNewButton.setIcon(new ImageIcon(SFLX.class.getResource("/com/ejia/photo/刷新.png")));
		btnNewButton.setBounds(225, 15, 199, 73);
		contentPane.add(btnNewButton);
		
		JButton button = new JButton("编辑类型");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRowCount()>0) {
					index=table.getSelectedRow();
					new BJLX().setVisible(true);
					dispose();
				} else {
					JOptionPane.showMessageDialog(null, "请选择要修改的数据");
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 24));
		button.setIcon(new ImageIcon(SFLX.class.getResource("/com/ejia/photo/编辑.png")));
		button.setBounds(439, 15, 199, 73);
		contentPane.add(button);
		
		JButton button_1 = new JButton("删除数据");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRowCount()>0) {
					index=table.getSelectedRow();
					int id=fytypes.get(index).getFyid();
					if (MtypeService.delete(id)) {
						JOptionPane.showMessageDialog(null, "删除成功");
						dataBind();
					} else {
						JOptionPane.showMessageDialog(null, "删除失败");
					}
				} else {
					JOptionPane.showMessageDialog(null, "请选择要删除的数据");
				}
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 24));
		button_1.setIcon(new ImageIcon(SFLX.class.getResource("/com/ejia/photo/删除好友.png")));
		button_1.setBounds(653, 15, 199, 73);
		contentPane.add(button_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 103, 869, 501);
		contentPane.add(scrollPane);
		
		table = new JTable();
		dataBind();
		table.setRowHeight(40);
		table.setFont(new Font("宋体", Font.PLAIN, 26));
		scrollPane.setViewportView(table);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new LineBorder(new Color(255,147,0)), "缴费类型管理", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 120, 215)));
		panel.setBounds(899, 15, 390, 589);
		
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("费用名称");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		lblNewLabel.setBounds(15, 59, 107, 35);
		panel.add(lblNewLabel);
		
		txtName = new JTextField();
		txtName.setFont(new Font("宋体", Font.PLAIN, 22));
		txtName.setBounds(131, 63, 244, 27);
		panel.add(txtName);
		txtName.setColumns(10);
		
		JLabel label = new JLabel("单价");
		label.setFont(new Font("宋体", Font.PLAIN, 24));
		label.setBounds(15, 123, 81, 21);
		panel.add(label);
		
		txtPrice = new JTextField();
		txtPrice.setFont(new Font("宋体", Font.PLAIN, 22));
		txtPrice.setColumns(10);
		txtPrice.setBounds(131, 120, 244, 27);
		panel.add(txtPrice);
		
		JLabel label_1 = new JLabel("单位");
		label_1.setFont(new Font("宋体", Font.PLAIN, 24));
		label_1.setBounds(15, 180, 81, 21);
		panel.add(label_1);
		
		txtUnit = new JTextField();
		txtUnit.setFont(new Font("宋体", Font.PLAIN, 22));
		txtUnit.setColumns(10);
		txtUnit.setBounds(131, 177, 244, 27);
		panel.add(txtUnit);
		
		JLabel label_2 = new JLabel("类型详细");
		label_2.setFont(new Font("宋体", Font.PLAIN, 24));
		label_2.setBounds(15, 232, 107, 21);
		panel.add(label_2);
		
		JTextPane textPane = new JTextPane();
		textPane.setFont(new Font("宋体", Font.PLAIN, 22));
		textPane.setBounds(25, 281, 340, 223);
		panel.add(textPane);
		
		JButton button_2 = new JButton("保存");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name=txtName.getText();
				double price=Double.parseDouble(txtPrice.getText());
				String unit=txtUnit.getText();
				String text=textPane.getText();
				if (MtypeService.add(name,price,unit,text)) {
					JOptionPane.showMessageDialog(null, "添加成功");
					dataBind();
				} else {
					JOptionPane.showMessageDialog(null, "添加失败");
				}
			
			}
		});
		button_2.setFont(new Font("宋体", Font.PLAIN, 32));
		button_2.setBounds(220, 519, 142, 53);
		panel.add(button_2);
		
		JButton button_3 = new JButton("返回");
		button_3.setFont(new Font("宋体", Font.PLAIN, 28));
		button_3.setIcon(new ImageIcon(SFLX.class.getResource("/com/ejia/photo/返回 (1).png")));
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Main().setVisible(true);
				dispose();
			}
		});
		button_3.setBounds(15, 15, 195, 73);
		contentPane.add(button_3);
		setLocationRelativeTo(null);
	}
}
